<?php
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');
class OrderAction extends AdministratorAction{

public $pageTitle = array();
	
	/**
	 * 初始化，初始化页面表头信息，用于双语
	 */
	public function _initialize() {
		$this->pageTitle['index'] = '所有约单';
		$this->pageTitle['typeIndex'] = '约单类型管理';
	

		parent::_initialize();
	}

	/**
	 * 约单类型列表   
	 */
	public function index()
	{
		$_REQUEST['tabHash'] = 'index';
		// 初始化约单列表管理菜单
		$this->_initOrderListAdminMenu('index');
		
		$listData = $this->_getOrderList('20', $map, 'index');
		
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->displayList($listData);
	}
	
	/**
	 * 所有约单列表
	 */
	public function typeIndex(){
		$_REQUEST['tabHash'] = 'typeIndex';
		// 初始化用户列表管理菜单
		$this->_initOrderListAdminMenu('typeIndex');
		
		$listData = $this->_getOrderList('20', $map, 'typeIndex');
		
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->displayList($listData);
		
	}

	/**
	 * 初始化约单列表管理菜单
	 * @param string $type 列表类型，index、pending、dellist
	 */
	private function _initOrderListAdminMenu($type) {
		// tab选项
		if($type == "index"){
			//所有约单栏目
			$this->pageTab[] = array('title'=>"所有约单",'tabHash'=>'index','url'=>U('admin/Order/index'));
			
		}elseif($type == "typeIndex"){
			$this->pageTab[] = array('title'=>"类型分类",'tabHash'=>'typeIndex','url'=>U('admin/Order/typeIndex'));
			$this->pageTab[] = array('title'=>"增加分类",'tabHash'=>'addType','url'=>U('admin/Order/addOrderType'));
			
		}
		
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
		
		
		
		// 列表key值 DOACTION表示操作
		switch(strtolower($type)) {
			case 'index':
				$this->pageKeyList = array('oid','ordername',"tid","end_time","people_limit","people_current","rule","ctime","uid",'DOACTION');
				break;
			case 'typeindex':
				$this->pageKeyList = array('tid','o_type',"pic",'DOACTION');
				break;
		}
		
/*		if(!empty($_POST['_parent_dept_id'])) {
			$this->onload[] = "admin.departDefault('".implode(',', $_POST['_parent_dept_id'])."','form_user_department')";
		}*/
	}

	/**
	 * 解析约单列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getOrderList($limit = 20, $map = array(), $type = 'index') {
		// 设置列表主键
		if($type == "index"){
			$this->_listpk = 'oid';
			// 取用户列表
			$listData['data'] = M('Order')->select();
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['ctime'] = date('Y-m-d H:i:s',$v['ctime']);
				$listData['data'][$k]['tid'] = M("Order_type")->where("tid = ".$listData['data'][$k]['tid'])->find()["o_type"];
				$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->find()["uname"];
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Order/editOrder',array('oid'=>$v['oid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.DelOrder(\''.$v['oid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			
			}
		}elseif($type == "typeIndex"){
			$this->_listpk = 'tid';
			// 取用户列表
			$listData['data'] = M('Order_type')->select();
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['pic'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['pic'] = getImageUrl($image_url);
				$listData['data'][$k]['pic'] = "<img style='height:50px;' src='".$listData['data'][$k]['pic']."'"." />";
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Order/editOrderType',array('tid'=>$v['tid'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.DelOrderType(\''.$v['tid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			
			}
		}
		
		
		
		
		return $listData;
	}
	
	/**
	 * 删除约单类型
	 */
	public function doDelOrderType(){
// 		$str = "";
// 		foreach ($_REQUEST as $k =>$v){
// 			$str .= $k."+".$v."/";
// 		}
// 		$return["data"] = $str;
		
		$tid = t($_REQUEST["tid"]);
		$result = M("Order_type")->where("tid = $tid")->delete();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_FAIL');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_SUCCESS');			// 操作成功
		}

		exit(json_encode($return));
	
	}
	
	//删除约单
	public function doDelOrder(){

		$oid = t($_REQUEST["oid"]);
		$result = M("Order")->where("oid = $oid")->delete();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_FAIL');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_REMOVE_COMPLETELY_SUCCESS');			// 操作成功
		}
		
		exit(json_encode($return));
		}
	/**
	 * 添加类型
	 */
	public function addOrderType() {
		$this->assign('pageTitle','添加分类');
		// 初始化用户列表管理菜单
		$this->_initOrderListAdminMenu();
		
        $this->pageKeyList = array('o_type','pic');
		$this->savePostUrl = U('admin/Order/doAddOrderType');
        $this->notEmpty = array('o_type','pic');
        $this->onsubmit = 'admin.OrderTypeCheck(this)';

		$this->displayConfig();
	}

	/**
	 * 添加类型操作
	 */
	public function doAddOrderType() {
		$order_type = M("Order_type");
		$order_type->o_type = t($_REQUEST["o_type"]);
		$order_type->pic = t($_REQUEST["pic"]);
		$result = $order_type->add();
		if($result) {
			$this->assign('jumpUrl', U('admin/Order/typeIndex'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		} else {
			$this->assign('jumpUrl', U('admin/Order/typeIndex'));
			$this->error('添加失败');
		}
	}

	/**
	 * 编辑类型信息页面
	 */
	public function editOrderType(){
		$this->assign('pageTitle','编辑约单类型');
		
		$this->pageKeyList = array('o_type','pic');
		
		$order = M('Order_type')->where('tid='.$_GET['tid'])->find();
		if($order['pic']){
			$image = D('attach')->where('attach_id='.$order['pic'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$order['pic_url'] = getImageUrl($image_url);
		}
		$this->notEmpty = array('o_type,pic');
		$this->savePostUrl = U('admin/Order/doEditOrderType',array("tid"=>$_GET['tid']));
		$this->onsubmit = 'admin.OrderTypeCheck(this)';
		$this->displayConfig($order);
	}

	/**
	 * 更新类型信息
	 */
	public function doEditOrderType() {
// 		echo t($_REQUEST["o_type"]);
//dump($_REQUEST);exit;
		$order_type =M("Order_type");
		
		
		$data['tid'] = t($_REQUEST["tid"]);
		$data['o_type']=t($_REQUEST["o_type"]);
		$data['pic']=t($_REQUEST["pic"]);
	//dump($order_type);exit;
		$result = $order_type->data($data)->save();//$sql ="update ts_order_type set o_type='".t($_REQUEST["o_type"])."' and pic = ".t($_REQUEST["pic"])."where tid = ".$_REQUEST['tid']
		
		if($result){
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error($order_type->getLastSql());
		}
		
	
	}

	
	/**
	 * 更新约单页面
	 * @return json 操作后的JSON数据
	 */
	public function editOrder() {
		$this->assign('pageTitle','编辑约单');
		
		$this->pageKeyList = array('ordername','tid',"end_time","people_limit","rule");
		$arr= M("Order_type")->select();
		$ot = array();
		foreach ($arr as $k=>$v){
			$ot[$v['tid']] =$v["o_type"];
			
		}
		$this->opt['tid'] = $ot;
		$order = M('Order')->where('oid='.$_GET['oid'])->find();
		$this->notEmpty = array('ordername','tid',"end_time","people_limit","rule");
		$this->savePostUrl = U('admin/Order/doEditOrder',array("oid"=>$_GET['oid']));
		$this->onsubmit = 'admin.OrderCheck(this)';
		$this->displayConfig($order);
	}

	/**
	 * 更新约单操作
	 * @return json 操作后的JSON数据
	 */
	public function doEditOrder() {
		//dump($_REQUEST);exit;
		$Order = M("Order");
		$data["oid"] = t($_REQUEST["oid"]);
		$data["ordername"] =t($_REQUEST["ordername"]);
		$data["tid"] =t($_REQUEST["tid"]);
		$data["end_time"] =t($_REQUEST["end_time"]);
		$data["people_limit"] =t($_REQUEST["people_limit"]);
		$data["rule"] = t($_REQUEST["rule"]);
		$result = $Order->data($data)->save();
		if($result){
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error($Order->getLastSql());
		}
		
	}

	
}